var utils = require('../../../../utils/util.js')
var c = require('../../../../utils/config.js')
var p = require('../../../../utils/password.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    slide:[],
    img_url: c.img_url,
    vipnotes:[],
    vipconfig:[],
    r_code:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  	if(wx.getStorageSync("userinfo").level){
  		wx.navigateTo({
          url: '/pages/cloudshop/index/index'
        })
  	}
    var that = this;
    var token = wx.getStorageSync("userinfo").wap_token;
    var data = {
      url: "/zfcom/slide/lists",
      cat_id: 13,
      keyword:"",
      page:1,
      limit:1
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        var data = res.data;
        that.setData({
          slide: data,
        });
      } else {

      }
    });

    var data = {
      url: "/zfcom/com/getword",
      uuid: "vipnotes",
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        var data = res.data;
        that.setData({
          vipnotes: data,
        });
      } else {

      }
    });

    var data = {
      url: "/zfshop/userlevelconfig/lists",
      token: token,
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        var data = res.data;
        that.setData({
          vipconfig: data[0],
        });
      } else {

      }
    });

    var data = {
      url: "/yluser/user/gettokenuserinfo",
      token: token,
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        var data = res.data;
        that.setData({
          r_code: data.relation_invitecode
        });
      } else {

      }
    });
  },
  editinvitecode: function (e) {
    this.setData({
      r_code: e.detail.value
    })
  },
  paypwd_btn: function () {
    var app = getApp();
    var user_sign = app.globalData.user_sign;
    if (!user_sign) {
      wx.showToast({
        title: '请签名',
        icon: "none"
      })
      setTimeout(function () {
        wx.navigateTo({
          url: '/pages/template/canvas/index'
        })
      }, 500);
      return false;
    };
    this.setData({
      is_show: true
    })
  },
  bindKeyInput: function (e) {
    let value = e.detail.value
    let key = e.target.dataset.index;
    this.setData({
      password: value
    })
  },
  close_pwd: function () {
    this.setData({
      is_show: false
    })
  },
  submit: function () {
    utils.checkLogin();
    var level_id = this.data.vipconfig.id;
    var pay_type = 0;//微信
    var app = getApp();
    var user_sign = app.globalData.user_sign;
    var that = this;
    if (!user_sign) {
      wx.showToast({
        title: '请签名',
        icon: "none"
      })
      setTimeout(function(){
        wx.navigateTo({
          url: '/pages/template/canvas/index'
        })
      },500);
      return false;
    };
    // if (that.data.r_code == ""){
    //   wx.showToast({
    //     title: '请填写邀请码',
    //     icon: "none"
    //   })
    //   return false;
    // }
    var paypwd = this.data.password;
    if (!paypwd || paypwd < 100000) {
      utils.errMsg("请输入支付密码");
      return false;
    }
    if (paypwd < 100000) {
      utils.errMsg("请输入支付密码");
      return false;
    }
    var token = wx.getStorageSync("userinfo").wap_token;
    
      var data = {
        url: "/zfshop/uservip/add",
        level_id: level_id,
        token: token,
        pay_type: pay_type,
        user_sign: user_sign,
        openid: wx.getStorageSync("openId"),
        invitecode: that.data.r_code,
        spassword: p.encryption(paypwd)
      };

    utils.request(data).then(function (res) {
      if (res.code == 0) {
        app.globalData.user_sign = "";
        //直接支付成功!
        if (res.data.length == 0) {
          utils.sucMsg("支付成功");
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 2000);
          return false;
        }
        //走在线支付
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            utils.sucMsg("支付成功");
            setTimeout(function () {
              wx.navigateBack({
                delta: 1,
              })
            }, 2000);
          },
          'fail': function (res) {
            utils.errMsg("支付失败");
            setTimeout(function () {
              wx.navigateBack({
                delta: 1,
              })
            }, 2000);
          }
        })
      } else {
        utils.errMsg(res.msg);
      }
    });
  }
})